// // Copyright (c) 2009 All Right Reserved // // vl // // 2009-01-01 // Contains ... using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Text; using System.Xml.Serialization; namespace LargoCommon.Music { /// General musical request. /// Musical object. [Serializable] [XmlRoot] public sealed class GeneralRequest : ICloneable { /// Properties and their values. private Dictionary items; #region Constructors /// Initializes a new instance of the GeneralRequest class. Serializable. public GeneralRequest() { this.items = new Dictionary(); this.RandomEffect = 0; } #endregion /// Gets properties and their values. /// Property description. [XmlIgnore] public Dictionary Items { get { Contract.Ensures(Contract.Result>() != null); if (this.items == null) { throw new InvalidOperationException("Null items."); } return this.items; } } /// Gets or sets random effect. /// Property description. public float RandomEffect { get; set; } #region Static operators //// TICS rule 7@531: Class GeneralOwner implements operator + but not operator ==. /// Operator + to join sets of properties. /// General musical request 1. /// General musical request 2. /// Returns value. public static GeneralRequest operator +(GeneralRequest request1, GeneralRequest request2) { Contract.Requires(request1 != null && request2 != null); if (request1 == null) { return null; } if (request2 == null) { return null; } var request = (GeneralRequest)request1.Clone(); request.AppendItems(request2.Items); return request; } //// TICS rule 7@526: Reference types should not override the equality operator (==) //// public static bool operator ==(GeneralRequest request1, GeneralRequest request2) { return object.Equals(request1, request2); } //// public static bool operator !=(GeneralRequest request1, GeneralRequest request2) { return !object.Equals(request1, request2); } //// but TICS rule 7@530: Class implements interface 'IComparable' but does not implement '==' and '!='. /// /// Add request. /// /// Musical request1. /// Musical request2. /// Returns value. public static GeneralRequest Add(GeneralRequest request1, GeneralRequest request2) { Contract.Requires(request1 != null && request2 != null); if (request1 == null || request2 == null) { return null; } var request = (GeneralRequest)request1.Clone(); request.AppendItems(request2.Items); return request; } #endregion /// Test of equality. /// Object to be compared. /// Returns value. public override bool Equals(object obj) { //// check null (this pointer is never null in C# methods) if (object.ReferenceEquals(obj, null)) { return false; } if (object.ReferenceEquals(this, obj)) { return true; } if (this.GetType() != obj.GetType()) { return false; } return this.CompareTo(obj) == 0; } /// Support sorting according to level and number. /// Object to be compared. /// Returns value. public int CompareTo(object value) { return value is GeneralRequest gr ? this.Items.Count.CompareTo(gr.Items.Count) : 0; //// This kills the DataGrid //// throw new ArgumentException("Object is not a GeneralOwner"); } /// Support of comparison. /// Returns value. public override int GetHashCode() { return this.Items == null ? 0 : this.Items.Count.GetHashCode(); } /// Makes a deep copy of the GeneralRequest object. /// Returns object. public object Clone() { var request = new GeneralRequest(); request.AppendItems(this.items); return request; } #region Items /// Property settings. /// General musical property. /// Weight of the request. /// Requested value. public void SetItem(GenProperty property, float weight, float? value) { //// if (this.Items == null) { return; } var ri = new GeneralRequestItem(property, weight, value); this.items[property] = ri; } /// Returns float value of given property (or zero). /// General musical property. /// Returns value. public bool HasItem(GenProperty property) { return this.Items != null && this.items.ContainsKey(property); } /// Returns float value of given property (or zero). /// General musical property. /// Returns value. public GeneralRequestItem GetItem(GenProperty property) { return this.Items[property]; //// return this.items.ContainsKey(property) ? this.Items[property] : null; } /// Property settings. /// Items of musical properties. public void CopyItems(Dictionary givenItems) { this.items = new Dictionary(); if (givenItems == null) { return; } foreach (var rde in givenItems) { this.items[rde.Key] = rde.Value; } } /// Property settings. /// Items of musical properties. public void AppendItems(Dictionary givenItems) { if (givenItems == null || this.items == null) { return; } foreach (var rde in givenItems) { this.items.Add(rde.Key, rde.Value); } } #endregion #region String representation /// String representation of the object. /// Returns value. public override string ToString() { //// if (this.Items == null) { return "Empty request"; } var s = new StringBuilder(); // ReSharper disable once LoopCanBePartlyConvertedToQuery //// .Where(key => this.Items[key] != null) .Where(key => this.Items[key] != null) foreach (var key in this.Items.Keys) { var item = this.Items[key]; //// if (item != null) { s.AppendLine(item.ToString()); //// } } return s.ToString(); } #endregion } } //// unused public GeneralRequest RhythmicalRequest(int barNumber) { //// CourseMobility, CourseFilling, CourseVariability